package com.gzwanchuang.lszg.module.machines;

/**
 * 文件名 : EarningDetailActivity
 * 创建者 : MangoWang
 * 创建日期 : 2019/11/28 20:06
 * 微信 : MangoWaWang
 * 邮箱 : 763482205@qq.com
 * 描述 : TODO
 */

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.gzwanchuang.lszg.R;
import com.gzwanchuang.lszg.base.BaseActivity;
import com.gzwanchuang.lszg.net.Apis;
import com.gzwanchuang.lszg.net.NetworkManager;
import com.gzwanchuang.lszg.net.interfaces.OnRequestCallBack;
import com.gzwanchuang.lszg.proto.Mystock;
import com.gzwanchuang.lszg.utils.StatusBarUtil;
import com.gzwanchuang.lszg.utils.ValidateUtils;
import com.ruffian.library.widget.RLinearLayout;

import java.text.SimpleDateFormat;
import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class AllowActivity extends BaseActivity {

    Unbinder unbinder;
    @BindView(R.id.back_iv)
    ImageView backIv;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_select_produce)
    TextView tvSelectProduce;
    @BindView(R.id.tv_select_sn_number)
    TextView tvSelectSnNumber;
    @BindView(R.id.tv_number)
    TextView tvNumber;
    @BindView(R.id.btn_submit)
    Button btnSubmit;
    @BindView(R.id.tv_title_right)
    TextView tvTitleRight;
    @BindView(R.id.ll_select_sn_number)
    RLinearLayout llSelectSnNumber;
    @BindView(R.id.rg_jiezhang)
    RadioGroup rgJiezhang;
    @BindView(R.id.tv_date_message)
    TextView tvDateMessage;
    @BindView(R.id.et_dan_jia)
    EditText etDanJia;
    @BindView(R.id.tv_fen_qi_three)
    TextView tvFenQiThree;
    @BindView(R.id.tv_fen_qi_six)
    TextView tvFenQiSix;
    @BindView(R.id.tv_fen_qi_nice)
    TextView tvFenQiNice;
    @BindView(R.id.tv_fen_qi_twelve)
    TextView tvFenQiTwelve;
    @BindView(R.id.ll_dan_jia)
    LinearLayout llDanJia;
    @BindView(R.id.ll_dai_kuan)
    LinearLayout llDaiKuan;
    @BindView(R.id.rb_three)
    RadioButton rbThree;
    @BindView(R.id.rb_six)
    RadioButton rbSix;
    @BindView(R.id.rb_nice)
    RadioButton rbNice;
    @BindView(R.id.rb_twelve)
    RadioButton rbTwelve;


    private String goods_id = "";
    private String sn_up_code = "";
    private String phone;
    private String type_hasJieSUAN = "0";
    private int type_jisuan = 0;
    private int type_fenqishu = 0; // 分期的月份数

    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.rb_three:
                    rbSix.setChecked(false);
                    rbNice.setChecked(false);
                    rbTwelve.setChecked(false);
                    type_fenqishu = 3;
                    break;
                case R.id.rb_six:
                    rbThree.setChecked(false);
                    rbNice.setChecked(false);
                    rbTwelve.setChecked(false);
                    type_fenqishu = 6;
                    break;
                case R.id.rb_nice:
                    rbSix.setChecked(false);
                    rbThree.setChecked(false);
                    rbTwelve.setChecked(false);
                    type_fenqishu = 9;
                    break;
                case R.id.rb_twelve:
                    rbThree.setChecked(false);
                    rbSix.setChecked(false);
                    rbNice.setChecked(false);
                    type_fenqishu = 12;
                    break;
            }
        }
    };
    private String memberid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_allow);
        unbinder = ButterKnife.bind(this);
        StatusBarUtil.setStatusBarLightMode(this, Color.parseColor("#ffffff"), true);
        tvTitle.setText("发起调拨");
        initListener();

    }

    private void initListener() {
        rbThree.setOnClickListener(onClickListener);
        rbSix.setOnClickListener(onClickListener);
        rbNice.setOnClickListener(onClickListener);
        rbTwelve.setOnClickListener(onClickListener);

        backIv.setOnClickListener(v -> {
            finish();
        });
        tvSelectProduce.setOnClickListener(view -> {
            if (ValidateUtils.isValidate(memberid)) {
                Intent intent = new Intent(mContext, AllocMachineActivity.class);
                intent.putExtra("title", "选择调拨产品");
                intent.setType("0");
                intent.putExtra("member_id",memberid);
                startActivityForResult(intent, 100);
            } else {
                showToast("请先选择调用伙伴");
            }
        });
        llSelectSnNumber.setOnClickListener(view -> {
            boolean empty = TextUtils.isEmpty(goods_id);
            if (empty) {
                showToast("请先选择调拨机器类型");
            } else {
                Intent intent = new Intent(mContext, AllowSnActivity.class);
                intent.putExtra("goods_id", goods_id);
                startActivityForResult(intent, 100);
            }

        });
        btnSubmit.setOnClickListener(view -> submit());
        tvName.setOnClickListener(view -> {
            Intent intent = new Intent(mContext, AllowPartnerActivity.class);
            intent.putExtra("title", "选择调拨的伙伴");
            intent.putExtra("type", "2");
            startActivityForResult(intent, 100);
        });
        rgJiezhang.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.rb_one) {
                    tvDateMessage.setText("*选择[已收全款]，系统将不会产生贷款。");
                    llDanJia.setVisibility(View.GONE);
                    type_jisuan = 0;
                } else {
                    llDanJia.setVisibility(View.VISIBLE);
                    type_jisuan = 1;
                    SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月");
                    SimpleDateFormat formatterTwo = new SimpleDateFormat("dd");
                    Date curDate = new Date(System.currentTimeMillis());
                    String str = formatter.format(curDate);
                    int hao = Integer.valueOf(formatterTwo.format(curDate));
                    if (hao > 28) {
                        tvDateMessage.setText("*系统将在" + str + "28日起每月28日向下级伙伴账户扣款，直至扣除全部贷款");
                    } else {
                        tvDateMessage.setText("*系统将在" + str + hao + "日起每月" + hao + "日向下级伙伴账户扣款，直至扣除全部贷款");
                    }
                }
            }
        });
        etDanJia.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() == 0) {
                    tvFenQiThree.setText("每期需收款0.00元");
                    tvFenQiSix.setText("每期需收款0.00元");
                    tvFenQiNice.setText("每期需收款0.00元");
                    tvFenQiTwelve.setText("每期需收款0.00元");
                } else {
                    // 获取调拨数量
                    if (tvNumber.getText().toString().isEmpty()) {
                        return;
                    }
                    int number = Integer.valueOf(tvNumber.getText().toString());
                    double danJia = Double.valueOf(s.toString());
                    double total = danJia * number;
                    String numberThree = String.format("%.2f", total / 3);
                    String numberSix = String.format("%.2f", total / 6);
                    String numberNice = String.format("%.2f", total / 9);
                    String numberTwelve = String.format("%.2f", total / 12);
                    tvFenQiThree.setText("每期需收款" + numberThree + "元");
                    tvFenQiSix.setText("每期需收款" + numberSix + "元");
                    tvFenQiNice.setText("每期需收款" + numberNice + "元");
                    tvFenQiTwelve.setText("每期需收款" + numberTwelve + "元");
                }
            }
        });
    }


    private void submit() {
        String name = tvName.getText().toString();
        if (TextUtils.isEmpty(name)) {
            showToast("请先选择调拨");
            return;
        }
        if (TextUtils.isEmpty(goods_id)) {
            showToast("请先选择调拨产品");
            return;
        }

        if (TextUtils.isEmpty(sn_up_code)) {
            showToast("请先选择SN号");
            return;
        }


        Mystock.initiateATransferNew.Builder builder = Mystock.initiateATransferNew.newBuilder();
        builder.setGoodsId(goods_id).setSnCode(sn_up_code).setMemberName(name).setMemberId(memberid)
                .setMemberMobile(phone).setNum(tvNumber.getText().toString());

//
        // 1.判断是否需要显示结算
        if (type_hasJieSUAN.equals("1")) {
            if (type_jisuan == 0)  // 不分期
            {
                builder.setType("1");
            } else if (type_jisuan == 1)  // 分期
            {
                String danjia = etDanJia.getText().toString();
                if (danjia.isEmpty()) {
                    showToast("请先填入单价");
                    return;
                }
                if (type_fenqishu == 0) {
                    showToast("请先选择分期数");
                    return;
                }
                builder.setType("2").setPeriodNum(String.valueOf(type_fenqishu)).setUnitPrice(danjia);
            }

        }
        NetworkManager.INSTANCE.post(Apis.initiateATransfer, builder.build().toByteArray(), new OnRequestCallBack() {
            @Override
            public void onOk(byte[] bytes) {

            }

            @Override
            public void onError(int errorCode, String errorMessage) {
                showToast(errorMessage);
                if (errorCode == 200) {
                    finish();
                }
            }
        });
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 200) {
            tvName.setText(data.getStringExtra("name"));
            phone = data.getStringExtra("phone");
            memberid = data.getStringExtra("id");
            type_hasJieSUAN = data.getStringExtra("show_bill");
            if (type_hasJieSUAN.equals("0")) {
                llDaiKuan.setVisibility(View.GONE);
            } else {
                llDaiKuan.setVisibility(View.VISIBLE);
            }
            tvSelectSnNumber.setText("");
            tvSelectProduce.setText("");
            tvNumber.setText("");
            sn_up_code = "";
            goods_id = "";

        } else if (resultCode == 300) {
            tvSelectProduce.setText(data.getStringExtra("goods_name"));
            goods_id = data.getStringExtra("goods_id");

        } else if (resultCode == 400) {
            tvSelectSnNumber.setText(data.getStringExtra("sn_code"));
            sn_up_code = data.getStringExtra("sn_up_code");
            tvNumber.setText(data.getStringExtra("sn_number"));
            etDanJia.setText("");
            tvFenQiThree.setText("每期需收款0.00元");
            tvFenQiSix.setText("每期需收款0.00元");
            tvFenQiNice.setText("每期需收款0.00元");
            tvFenQiTwelve.setText("每期需收款0.00元");
        }
    }

}
